<?php

namespace app\admin\controller;
use app\common\controller\Backend;
use think\helper\Time;

//use app\admin\model\Bus;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Busrecord extends Backend
{
    
    /**
     * Student模型对象
     * @var \app\admin\model\Student
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Busrecord;
        $this->bus_model = new \app\admin\model\Bus;
    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    
    public function index()
    {
        if($this -> request -> isPost()){
            $date = $this -> request -> post('create_time');
            $timestamp = strtotime($date);
            $timeArr = [$timestamp,$timestamp+(24*3600)-1];
            $this -> assign('date',$date);
        }else{
//            $timeArr = Time::today();
//            $this -> assign('date',date('Y-m-d'));
            $date = date("Y-m-d",strtotime("+1 day"));
            $timestamp = strtotime($date);
            $timeArr = [$timestamp,$timestamp+(24*3600)-1];
            $this -> assign('date',date("Y-m-d",strtotime("+1 day")));
        }

        $busrecord_list = $this->bus_model->alias('b')
            ->join('bus_record r','b.times=r.times','left')
            -> whereBetween('r.check_time',$timeArr)
            ->group('b.times')
            ->order('b.times,b.type')
            ->field('b.*,r.id as r_id,r.check_time')
            ->select();
        if(!empty($busrecord_list)&&is_array($busrecord_list))
        {
            foreach ($busrecord_list as $k =>$value)
            {
                $where['check_time'] = $value['check_time'];
                $where['time_slot'] = $value['id'];
                $count = $this->model->where($where)->count();
                $busrecord_list[$k]['count'] = $count;
            }
        }
        $busrecord_list1 = $this->bus_model->alias('b')
            ->join('bus_record r','b.times=r.times2','left')
            -> whereBetween('r.check_time',$timeArr)
            ->group('b.times')
            ->order('b.times,b.type')
            ->field('b.*,r.id as r_id,r.check_time')
            ->select();
        if(!empty($busrecord_list1)&&is_array($busrecord_list1))
        {
            foreach ($busrecord_list1 as $k =>$value)
            {
                $where1['time_slot2'] =$value['id'];
                $where1['check_time'] = $value['check_time'];
                $count = $this->model->where($where1)->count();
                $busrecord_list1[$k]['count'] = $count;
            }
        }
        $this -> assign('busrecord_list',$busrecord_list);
        $this -> assign('busrecord_count',count($busrecord_list));

        $this -> assign('busrecord_list1',$busrecord_list1);
        $this -> assign('busrecord_count1',count($busrecord_list1));
        return $this->view->fetch();
    }

    public function  detail($ids = null,$type = null)
    {
        $row = $this->model->get($ids);
        if(!$row)
        {
            $this->error('暂无记录');
        }
        $where['check_time'] = $row['check_time'];
        if($type==1){
            $where['time_slot'] = $row['time_slot'];
        }
        if($type==2){
            $where['time_slot2'] = $row['time_slot2'];
        }
        $list = $this->model->where($where)->select();
        if(!empty($list)&&is_array($list))
        {
            foreach ($list as $k =>$value)
            {
                $bus2 = $this->bus_model->where(['id'=>$value['time_slot2']])->find();
                $list[$k]['address2'] = $bus2['address'];

            }
        }
        $this -> assign('list',$list);
        return $this->view->fetch();
    }
}
